package c.x.jy.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import c.x.jy.common.annotation.Login;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.service.CmsArticleDataService;
import c.x.jy.service.service.CmsCategoryService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import c.x.jy.dao.entity.CmsArticleEntity;
import c.x.jy.service.service.CmsArticleService;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;



/**
 * 
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-05-22 16:38:13
 */
@RestController
@RequestMapping("cms/cmsarticle")
public class CmsArticleController extends AbstractController {
    @Autowired
    private CmsArticleService cmsArticleService;


    @Autowired
    private CmsCategoryService cmsCategoryService;

    @Autowired
    private CmsArticleDataService cmsArticleDataService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("cms:cmsarticle:list")
    public R list(@RequestParam Map<String, Object> params){
     	if(getUser().getBusinessId()>0) {
    		params.put("businessId", getUser().getBusinessId());
    	}
        PageUtils page = cmsArticleService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cms:cmsarticle:info")
    public R info(@PathVariable("id") Integer id){
		CmsArticleEntity cmsArticle = cmsArticleService.getById(id);

        return R.put(cmsArticle);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("cms:cmsarticle:save")
    public R save(@RequestBody CmsArticleEntity cmsArticle){
        if(getUser().getBusinessId()==0) {
            return R.error("请用商家帐号添加");
        }
        if(cmsArticle.getId()==null) {
            System.out.println(getUser());
            cmsArticle.setCreateBy(getUser().getUsername());
            cmsArticle.setCreateDate(new Date());

            cmsArticle.setBusinessId(getUser().getBusinessId());
            cmsArticleService.save(cmsArticle);
        }else {
            cmsArticle.setUpdateBy(getUser().getUsername() );
            cmsArticle.setUpdateDate(new Date());
            cmsArticle.setBusinessId(null);
            cmsArticleService.updateById(cmsArticle);

        }
       // cmsArticleService.save(cmsArticle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("cms:cmsarticle:update")
    public R update(@RequestBody CmsArticleEntity cmsArticle){
		cmsArticleService.updateById(cmsArticle);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("cms:cmsarticle:delete")
    public R delete(@PathVariable("id") Integer id){

        CmsArticleEntity c=     	cmsArticleService.getById(id);
        if(c!=null&&c.getArticleDataId()!=null) {

            cmsArticleDataService.removeById(c.getArticleDataId());
        }
		cmsArticleService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

